package com.oa.ylms.apps.dao.plan.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.constants.message.IMessageInfoStates;
import com.oa.ylms.apps.dao.plan.IMessageInfoDao;
import com.oa.ylms.apps.entity.plan.MessageInfoValue;

public class MessageInfoDaoImpl extends HibernateDaoSupport implements IMessageInfoDao {
	private List<MessageInfoValue> list;
	private int numbers;

	@Override
	public int addMessageInfo(MessageInfoValue messageInfo) throws Exception {
		if(messageInfo != null){
			this.getHibernateTemplate().save(messageInfo);
			return messageInfo.getMessageId();
		}
		return 0;
	}

	@Override
	public boolean deleteMessageInfo(MessageInfoValue messageInfo)
			throws Exception {
		if(messageInfo != null){
			try{
				this.getHibernateTemplate().delete(messageInfo);
			}catch(Exception e){
				return false;
			}
			return true;
		}
		return false;
	}

	@Override
	public boolean modifyMessageInfo(MessageInfoValue messageInfo)
			throws Exception {
		if(messageInfo != null){
			try{
				this.getHibernateTemplate().update(messageInfo);
			}catch(Exception e){
				return false;
			}
			return true;
		}
		return false;
	}

	@Override
	public MessageInfoValue queryByMessageId(MessageInfoValue messageInfo)
			throws Exception {
		if(messageInfo != null){
			return (MessageInfoValue)this.getHibernateTemplate().get(MessageInfoValue.class, messageInfo.getMessageId());
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MessageInfoValue> queryMessageInfoWithPage(final MessageInfoValue messageInfo ,final int rows ,final int pageNo)
			throws Exception {
		String hqlStr = "FROM MessageInfoValue miv WHERE 1=1";
		if(messageInfo.getToStaffId() != null){
			hqlStr += " AND miv.toStaffId = ?";
		}
		if(messageInfo.getStates() != IMessageInfoStates.UNDEFINED){
			hqlStr += " AND miv.states = ?";
		}
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if(messageInfo.getToStaffId() != null){
					query.setEntity(++index, messageInfo.getToStaffId());
				}
				if(messageInfo.getStates() != IMessageInfoStates.UNDEFINED){
					query.setInteger(++index, messageInfo.getStates());
				}
				query.setFirstResult(rows * (pageNo - 1));
				query.setMaxResults(rows);
				list = query.list();
				return list;
			}
		});
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int countTPage(final MessageInfoValue messageInfo, int rows)
			throws Exception {
		int pageNumbers = 0;
		String hqlStr = "FROM MessageInfoValue miv WHERE 1=1";
		if(messageInfo.getToStaffId() != null){
			hqlStr += " AND miv.toStaffId = ?";
		}
		if(messageInfo.getStates() != IMessageInfoStates.UNDEFINED){
			hqlStr += " AND miv.states = ?";
		}
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if(messageInfo.getToStaffId() != null){
					query.setEntity(++index, messageInfo.getToStaffId());
				}
				if(messageInfo.getStates() != IMessageInfoStates.UNDEFINED){
					query.setInteger(++index, messageInfo.getStates());
				}
				list = query.list();
				return list;
			}
		});
		this.numbers = list.size();
		if (numbers <= rows) {
			pageNumbers = 1;
		}
		if (numbers == 0) {
			pageNumbers = 0;
		} else {
			if (numbers % rows == 0) {
				pageNumbers = numbers / rows;
			} else {
				pageNumbers = numbers / rows + 1;
			}
		}
		return pageNumbers;
	}
	
	@SuppressWarnings("unchecked")
	public int getCount(final MessageInfoValue messageInfo) throws Exception {
		String hqlStr = "FROM MessageInfoValue miv WHERE 1=1";
		if(messageInfo.getToStaffId() != null){
			hqlStr += " AND miv.toStaffId = ?";
		}
		if(messageInfo.getStates() == IMessageInfoStates.UNREAD){
			hqlStr += " AND miv.states = ?";
		}
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if(messageInfo.getToStaffId() != null){
					query.setEntity(++index, messageInfo.getToStaffId());
				}
				if(messageInfo.getStates() == IMessageInfoStates.UNREAD){
					query.setInteger(++index, IMessageInfoStates.UNREAD);
				}
				list = query.list();
				return list;
			}
		});
		return list.size();
	}
	
	public int getTotalNumber() throws Exception {
	
		return numbers;
	}
	
	public List<MessageInfoValue> getList() {
		return list;
	}

	public void setList(List<MessageInfoValue> list) {
		this.list = list;
	}

	public int getNumbers() {
		return numbers;
	}

	public void setNumbers(int numbers) {
		this.numbers = numbers;
	}


}
